import {
    getToken,
    setToken,
    removeToken
} from '@/utils/auth'
import { getAsyncRoutes } from '@/utils/asyncRouter.js'
import { resetRouter } from '@/router'
import { login, getInfo, logout } from '@/api/auth/login'

const getDefaultState = () => {
    return {
        token: getToken(),
        name: '',
        avatar: '',
        routes: [],
        addRoutes: []
    }
}

const state = getDefaultState()

const routers = [{
        path: '/',
        name: 'index',
        meta: {
            title: "管理后台",
        },
        redirect: "/dashboard",
        component: "Layout",
        children: [{
                path: '/dashboard',
                name: 'Dashboard',
                meta: {
                    title: "仪表盘",
                    icon: 'desktop-outlined',
                },
                component: "RouteView",
                redirect: '/dashboard/workspace',
                children: [
                    {
                        path: '/dashboard/workspace',
                        name: 'Workspace',
                        meta: {
                            title: "工作台",
                            icon: 'table-outlined',
                        },
                        component: 'Home',
                    }
                ]
            },
            {
                path: '/account',
                name: 'Account',
                meta: {
                    title: "用户管理",
                    icon: 'team-outlined',
                },
                component: "RouteView",
                redirect: '/account/list',
                children: [
                    {
                        path: '/account/list',
                        name: 'AccountList',
                        meta: {
                            title: "用户列表",
                            icon: 'table-outlined',
                        },
                        component: 'AccountList',
                    }
                ]
            },
            {
                path: '/gen',
                name: 'Gen',
                meta: {
                    title: "代码管理",
                    icon: 'setting-outlined',
                },
                component: "RouteView",
                redirect: '/gen/dbConfig',
                children: [
                    {
                        path: '/gen/dbConfig',
                        name: 'DbConfig',
                        meta: {
                            title: "数据库管理",
                            icon: 'table-outlined',
                        },
                        component: 'DbConfigList',
                    }
                ]
            }
        ]
    }
]

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_ROUTES: (state, routes) => {
        state.addRoutes = routes
        state.routes = routes
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    }
}

const actions = {
    // user login
    login({
        commit
    }, userInfo) {
        return new Promise((resolve, reject) => {
            login(userInfo).then(response => {
                const token = response.data
                commit('SET_TOKEN', token)
                setToken(token)
                resolve()
              }).catch(error => {
                reject(error)
              })
        })
    },

    // get user info
    getInfo({
        commit
    }) {
        return new Promise((resolve, reject) => {
            getInfo().then(response => {
                const result = response.data
                commit('SET_NAME', result.username)
                commit('SET_AVATAR', result.avatar)
                resolve()
              }).catch(error => {
                reject(error)
              })
        })
    },

    generateRoutes({
        commit
    }) {
        return new Promise(resolve => {
            let accessedRoutes = getAsyncRoutes(routers);
            // let accessedRoutes = routers;
            commit('SET_ROUTES', accessedRoutes)
            resolve(accessedRoutes)
        })
    },

    // user logout
    logout({ commit }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          removeToken()
          resetRouter()
          commit('RESET_STATE')
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // remove token
    resetToken({ commit }) {
      return new Promise(resolve => {
        removeToken()
        commit('RESET_STATE')
        resolve()
      })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}